using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace CMS_SUN_IF_Extraction
{
    public partial class frmMain_SysControl : Form
    {
       /* private DataGridViewComboBoxCell cboVATcenter1 = null;
        private DataGridViewComboBoxCell cboVATcenter2 = null;
        private DataGridViewComboBoxCell cboVATcenter3 = null;
        private DataGridViewComboBoxCell cboVATcenter4 = null;
        private DataGridViewComboBoxCell cboVATcenter5 = null;*/

        private BAL bao = new BAL();
        private DataTable tblMain = null;
        private DataTable tblVATCenter = null;
        private ArrayList lstCBCell = new ArrayList();
        private string oldStartdate = "";
        private string oldEndDate = "";
         public frmMain_SysControl()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (uValid_Changes_OK())
            {
                update_DB();
                this.Close();
            }
            else
                return;
        }

        private void update_DB()
        {
            DataRow r = tblMain.Rows[0];
            r["Ctrl_Start_Date"] = dtpStartDate.Value.ToShortDateString();
            r["Ctrl_End_Date"] = dtpEndDate.Value.ToShortDateString();
            r["Ctrl_Account_period"] = Int32.Parse(txtAcctPeriod.Text.Trim());

            for (int i = 1; i <= 5;i++ )
            {
                r["Ctrl_VAT" + i + "_Center"] = dtgVATCenCtrlRange.Rows[0].Cells[i - 1].Value != null?dtgVATCenCtrlRange.Rows[0].Cells[i - 1].Value.ToString():"";
                r["Ctrl_VAT" + i + "_StartNum"] = dtgVATCenCtrlRange.Rows[1].Cells[i - 1].Value.ToString();
                r["Ctrl_VAT" + i + "_EndNum"] = dtgVATCenCtrlRange.Rows[2].Cells[i - 1].Value.ToString();
            }
            tblMain.AcceptChanges();
            int tt = bao.update_DB(tblMain,oldStartdate,oldEndDate);
        }

        private bool uValid_Changes_OK()
        {           
            if (dtpStartDate.Value == null || dtpEndDate.Value == null) 
            {
                MessageBox.Show("Please specify a START/END Control date !!");
                dtpStartDate.Focus();
                return false;
            }

            if (dtpStartDate.Value > DateTime.Now)
            {
                MessageBox.Show("Control START date can not be > than Today's date !!");
                dtpStartDate.Focus();
                return false;
            }
            
            if (dtpEndDate.Value > DateTime.Now)
            {
                MessageBox.Show("Control END date can not be > than Today's date !!");
                dtpEndDate.Focus();
                return false;
            }

            if (dtpEndDate.Value < dtpStartDate.Value)
            {
                MessageBox.Show("START Date must not > END Date !!");
                return false;
            }

            if  (txtAcctPeriod.Text.Trim()== "" || bao.isNumeric(txtAcctPeriod.Text.Trim())==false)
            {
                MessageBox.Show("Please specify an Accounting Period !!");
                return false;
            }
            // ------------- checking the VAT STARt/END number
            // check for VAT Center #1
            DataGridViewCell cell1,cell2,cell3;           
            foreach (DataGridViewColumn col in dtgVATCenCtrlRange.Columns)
            {
                cell1 = dtgVATCenCtrlRange.Rows[0].Cells[col.Name];
                cell2 = dtgVATCenCtrlRange.Rows[1].Cells[col.Name];
                cell3 = dtgVATCenCtrlRange.Rows[2].Cells[col.Name];
                if (cell1.Value != null && cell1.Value.ToString()!="")
                {                    
                    if (cell2.Value == null || cell2.Value.ToString().Trim() == "")
                    {
                        MessageBox.Show("The START and END Number can not be blank !!");
                        cell2.Selected = true;
                        dtgVATCenCtrlRange.CurrentCell = cell2;
                        return false;
                    }
                    else if (cell3.Value != null && bao.isNumeric(cell2.Value.ToString())==true && bao.isNumeric(cell3.Value.ToString()))
                    {
                        if (int.Parse(cell2.Value.ToString()) > int.Parse(cell3.Value.ToString()))
                        {
                            MessageBox.Show("START VAT# must not > than END VAT# !!!");
                            cell2.Selected = true;
                            dtgVATCenCtrlRange.CurrentCell = cell2;
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        private void init_DataGridView()
        {
            createHeader();
            dtgVATCenCtrlRange.Rows.Add(3);
            tblVATCenter = bao.get_lst_VAT_center();
            //DataGridViewRow rCbo = dtgVATCenCtrlRange.Rows[0];

            for (int i = 0; i < 3; i++)
            {
                DataGridViewRow row = dtgVATCenCtrlRange.Rows[i];
                for (int j = 0; j < 5; j++)
                {
                    if (i == 0)
                    {
                        DataGridViewComboBoxCell cllcbo = new DataGridViewComboBoxCell();
                        cllcbo.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                        cllcbo.DataSource = tblVATCenter;
                        cllcbo.DisplayMember = "VAT Center";
                        cllcbo.ValueMember = "VAT Center";
                        try
                        {
                            //if (checkExist(tbl,row.Cells[j].Value.ToString())== true)
                                row.Cells[j] = cllcbo;                            
                        }
                        catch (Exception ex)
                        {
                            lstCBCell.Add(j);
                        }
                        //lstCBCell.Add(cllcbo);
                    }
                    else
                    {
                        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                        cell.MaxInputLength = 5;
                        row.Cells[j] = cell;
                    }
                }
            }
        }

        private bool checkExist(DataTable tbl, string vatCenter)
        {
            
            DataRow[] rows = tbl.Select("[VAT Center]='" + vatCenter + "'");
            if (rows == null || rows.Length <= 0)
                return false;
            return true;
        }

        private void createHeader()
        {
            dtgVATCenCtrlRange.Columns.Add("0", "Center #1");
            dtgVATCenCtrlRange.Columns.Add("1", "Center #2");
            dtgVATCenCtrlRange.Columns.Add("2", "Center #3");
            dtgVATCenCtrlRange.Columns.Add("3", "Center #4");
            dtgVATCenCtrlRange.Columns.Add("4", "Center #5");

            dtgVATCenCtrlRange.RowHeadersVisible = false;

            foreach (DataGridViewColumn col in dtgVATCenCtrlRange.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }            
        }

        private void frmMain_SysControl_Load(object sender, EventArgs e)
        {
            tblMain = bao.get_tbl_SysControl();
            if (tblMain == null)
                return;                        
            //init_data(tblMain);
            DataRow r = tblMain.Rows[0];
            dtpStartDate.Value = DateTime.Parse(r["Ctrl_Start_Date"].ToString());           
            dtpEndDate.Value = DateTime.Parse(r["Ctrl_End_Date"].ToString());

            oldStartdate = r["Ctrl_Start_Date"].ToString();
            oldEndDate = r["Ctrl_End_Date"].ToString();

            txtAcctPeriod.Text = r["Ctrl_Account_period"].ToString();
           // dataGridView1.DataSource = tblMain;
            
            init_DataGridView();

            string startNum = "";
            string endNum = "";
            for (int i = 1; i <= 5; i++)
            {               
               // dtgVATCenCtrlRange.Rows[0].Cells[i-1].Value = r["Ctrl_VAT" + i + "_Center"].ToString();
                string ctrl_vat_center = r["Ctrl_VAT" + i + "_Center"].ToString();
                if (checkExist(tblVATCenter,ctrl_vat_center)==true)
                    dtgVATCenCtrlRange.Rows[0].Cells[i - 1].Value = r["Ctrl_VAT" + i + "_Center"].ToString();

                startNum = ("00000" + r["Ctrl_VAT" + i + "_StartNum"].ToString());
                dtgVATCenCtrlRange.Rows[1].Cells[i - 1].Value = startNum.Substring(startNum.Length - 5);
                endNum = ("00000" + r["Ctrl_VAT" + i + "_EndNum"].ToString());
                dtgVATCenCtrlRange.Rows[2].Cells[i - 1].Value = endNum.Substring(endNum.Length - 5);                                            
            }
        }

        private void init_data(DataTable tblMain)
        {
            DataRow r = tblMain.Rows[0];
            for (int i = 0; i < 5; i++)
            {
                //cboVATcenter1
            }
        }

        private void dtpStartDate_Leave(object sender, EventArgs e)
        {
            if (dtpStartDate.Value > DateTime.Now) 
            {
                MessageBox.Show("Control date can not be > than today's date !!");
                dtpStartDate.Focus();
            }
            else
                if (dtpEndDate.Value < dtpStartDate.Value)
                    dtpStartDate.Focus();
        }

        private void dtpEndDate_Leave(object sender, EventArgs e)
        {
            if (dtpEndDate.Value > DateTime.Now)
            {
                MessageBox.Show("Control date can not be > than today's date !!");
                dtpEndDate.Focus();
            }
            else if (dtpEndDate.Value < dtpStartDate.Value)
                dtpStartDate.Focus();
        }

        private void dtgVATCenCtrlRange_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

            if (dtgVATCenCtrlRange.RowCount == 0)
                return;

            DataGridViewColumn curCol = dtgVATCenCtrlRange.CurrentCell.OwningColumn;
            if (IsTextBoxCell(e.RowIndex))
            {
                DataGridViewTextBoxCell currCell = (DataGridViewTextBoxCell)dtgVATCenCtrlRange.CurrentCell;
                if (bao.isNumeric(currCell.Value.ToString()) == false)
                {
                    MessageBox.Show("Please input numerical character!", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    currCell.Value = "";
                    currCell.Selected = true;
                    //dtgSubmit.CurrentCell = currCell;
                    return;
                }
                string t = "00000" + currCell.Value.ToString();
                currCell.Value = t.Substring(t.Length - 5);
            }          
        }
        private bool IsTextBoxCell(int rowIdx)
        {
            if (rowIdx > 0)
                return true;
            return false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }

        private void txtAcctPeriod_Leave(object sender, EventArgs e)
        {
            string t = "0000000" + txtAcctPeriod.Text.Trim();
            txtAcctPeriod.Text = t.Substring(t.Length - 7);
        }

        private void txtAcctPeriod_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtAcctPeriod_Leave(sender, new EventArgs());
            }
        }
    }
}